window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		U.fillHead();
		U.fillFooter();
		var keyword = U.getParam("keyword");
		var search_type = U.getParam("search_type");
		var typ = 0;
		var orderBy = 'id';
		var curPage = 1;
		var state=0;
		var pageSize = 9;
		initPage();

		function initPage() { //初始化页面
			U.loadOpt(false);
			if(search_type == "众筹") {
				$(".type_result>span:last-child>span").eq(5).click();
			}
			get_donateList();
			initEvent();
		}

		function initEvent() {

			$('.status_list span').click(function() {//状态查询
				$('.status span').css('color', 'black');
				$(this).css('color', 'red');
				state=$(this).attr('state');
				get_donateList();
			});
			
			$('.type_list span').click(function() {//类型查询
				$('.type span').css('color', 'black');
				$(this).css('color', 'red');
				typ=$(this).attr('typ');
				get_donateList();
			});
			$('.orderlist span').click(function() {//排序查询
				$('.orderby span').css('color', 'black');
				$(this).css('color', 'red');
				orderBy=$(this).attr('typ');
				get_donateList();
			});

		}

		function get_donateList() {
			var params = { 'title': keyword, 'typ': typ, 'orderBy': orderBy,  'state':state, 'curPage': curPage, 'pageSize': pageSize }
			$('.donate_list').html('');
			U.ajax('/crowd/list', params, function(json) {
				if(json.success) {
					var result = json.models.crowdList;
					nextPage = json.models.page.nextPage;
					$(result).each(function() {
						var state = this.state;
						if(state == 1) { // 2.weizhankai.png 3.xiangmujieshu.png  1.jingxinzhong.png
							state = '未开始';
						} else if(state == 2) {
							state = '进行中';
						} else {
							state = '已结束';
						}
						var typ = this.typ;
						var type = this.typ;
						if(typ == 1) {
							typ = '众筹';
						} else {
							typ = '捐款';
						}
						var days = U.getNum(U.getMyDate(this.endTime));
						var money = this.totalAmount / this.amount * 100;
						$('.donate_list').append('<div id=' + this.id + ' type=' + type + ' class="donate"><div><img class="poor_img" src="' + U.getImg(this.images) + '"></div><div class="title"><span>' + this.title + '</span><span>' + state + '</span></div><div style="border:1px solid #E5E6E6;border-radius:15px;width:100%;height:20px"></div><div class="state_img" style="width:' + money + '%"></div><ul class="detail_menu"><li>' + money + '%<p>已达</p></li><li>￥' + this.totalAmount + '<p>已筹款</p></li><li>' + days + '天<p>剩余时间</p></li></ul></div>');
					});
					$(".donate_list>div").width((($(".donate_list").width()) - 20 * 4) / 3);
					paging_handle(json.models.page, get_donateList);
				} else {
					M.toast(false, json.errorMsg);
				}
				$('.donate').click(function() {
					location.href = 'donate_result.html?id=' + $(this).attr('id') + '&type=' + $(this).attr('type');
				});
			});

		}

		function paging_handle(page, method) {
			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;

			if(lastsize >= totalPage) {
				lastsize = totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!")
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕
		}
	});
}